<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <link rel="stylesheet" href="styles/style.css">
        <link rel="stylesheet" href="styles/theme/atom-one-dark.css">
        <title>例子7-JS进阶（一）</title>
    </head>
    <body class="cols">
        <div class="cols">
            <pre>
                <code>
                        // 例子7

                        // ①
                        var promise1 = new Promise(function(resolve, reject) {
                                setTimeout(resolve, 1000, 1);
                        })
                        var promise1_1 = promise1.then(function(value) {
                                return value + 5;
                        });
                        var promise1_2 = promise1_1.then(function(value) {
                                console.log(value);
                                console.log(promise1_1);
                        })
                </code>
            </pre>
            <pre>
                <code>
                        // ②
                        var promise2 = new Promise(function(resolve, reject) {
                                setTimeout(resolve, 1000, 2);
                        })
                        var promise2_1 = promise2.then(value => {
                                throw '出现错误';
                        });
                        var promise2_2 = promise2_1.then(function(value) {
                                console.log(value);
                        }, function(err) {
                                console.log(err);
                                console.log(promise2_1);
                        });

                </code>
            </pre>
            <pre>
                <code>
                        // ③
                        var promise3 = new Promise(function(resolve, reject) {
                                setTimeout(resolve, 1000, 3);
                        })
                        var promise3_1 = promise3.then(function(value) {
                                console.log(promise3);
                                return promise3;
                        });
                        var promise3_2 = promise3_1.then(function(value) {
                                console.log(value);
                                console.log(promise3_1);
                        });
                </code>
            </pre>
        </div>
        <div class="cols">
            <pre>
                <code>
                        // ④
                        var promise4 = new Promise(function(resolve, reject) {
                                setTimeout(reject, 1000, '出现错误');
                        })
                        var promise4_1 = promise4.then(function(value) {
                                console.log(value)
                        }, function(err) {
                                console.log(promise4);
                                return promise4;
                        });
                        var promise4_2 = promise4_1.then(function(value) {
                                console.log(value);
                        }, function(err) {
                                console.log(err);
                                console.log(promise4_1);
                        });
                </code>
            </pre>
            <pre>
                <code>
                        // ⑤
                        var promise5_1_1;
                        var promise5 = new Promise(function(resolve, reject) {
                                setTimeout(resolve, 1000, 5);
                        })
                        var promise5_1 = promise5.then(function(value) {
                                promise5_1_1 = new Promise(function(resolve, reject) {
                                        setTimeout(resolve, 5000, 10);
                                })
                                console.log(promise5_1_1);
                                console.log(promise5_1);
                                return promise5_1_1;
                        });
                        var promise5_2 = promise5_1.then(function(value) {
                                console.log(value);
                                console.log(promise5_1_1);
                                console.log(promise5_1);
                        });

                </code>
            </pre>
        </div>
        <script src="scripts/libs/highlight.pack.js"></script>
        <script src="scripts/7.js"></script>
        <script>
            hljs.initHighlightingOnLoad();
        </script>
    </body>
</html>